package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.domain.model.LoginUser;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.enums.UpDownShelvesEnum;
import com.member.common.utils.StringUtils;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizCarousel;
import com.member.shop.domain.SysBizFeedback;
import com.member.shop.service.ISysBizCarouselService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【轮播图管理】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-12-26
 **/
@RestController
@RequestMapping("/shop/carousel")
public class SysBizCarouselController extends BaseController {
    @Resource
    private ISysBizCarouselService sysBizCarouselService;

    /**
     * 查询轮播图管理列表
     */
    @PreAuthorize("@ss.hasPermi('shop:carousel:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizCarousel sysBizCarousel) {
        startPage();
        setTenantId(sysBizCarousel);
        List<SysBizCarousel> list = sysBizCarouselService.selectSysBizCarouselList(sysBizCarousel);
        return getDataTable(list);
    }

    /**
     * 导出轮播图管理列表
     */
    @PreAuthorize("@ss.hasPermi('shop:carousel:export')")
    @Log(title = "轮播图管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizCarousel sysBizCarousel) {
        setTenantId(sysBizCarousel);
        LambdaQueryWrapper<SysBizCarousel> queryWrapper = new LambdaQueryWrapper<SysBizCarousel>();
        if (sysBizCarousel.getType() != null) {
            queryWrapper.eq(SysBizCarousel::getType, sysBizCarousel.getType());
        }
        queryWrapper.eq(SysBizCarousel::getTenantId, sysBizCarousel.getTenantId());
        queryWrapper.like(StringUtils.isNotEmpty(sysBizCarousel.getTitle()), SysBizCarousel::getTitle, sysBizCarousel.getTitle());
        queryWrapper.orderByDesc(SysBizCarousel::getCreateTime);
        queryWrapper.eq(SysBizCarousel::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());

        List<SysBizCarousel> list = sysBizCarouselService.selectSysBizCarouselList(queryWrapper);
        ExcelUtil<SysBizCarousel> util = new ExcelUtil<SysBizCarousel>(SysBizCarousel.class);
        util.exportExcel(response, list, "轮播图管理数据");
    }

    /**
     * 获取轮播图管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:carousel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizCarouselService.selectSysBizCarouselById(id));
    }
    private void setTenantId(SysBizCarousel sysBizCarousel) {
        LoginUser loginUser = getLoginUser();
        Long tenantId = loginUser.getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizCarousel.setTenantId(tenantId);
        }
    }

    /**
     * 新增轮播图管理
     */
    @PreAuthorize("@ss.hasPermi('shop:carousel:add')")
    @Log(title = "轮播图管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizCarousel sysBizCarousel) {
        if (StringUtils.isNotEmpty(sysBizCarousel.getTitle())) {
            if (sysBizCarousel.getDisplayed().equals(UpDownShelvesEnum.GROUNDING.getKey()) && !sysBizCarouselService.selectGroundingNum(sysBizCarousel) && sysBizCarousel.getType().equals(0)) {
                return warn("轮播图上架最多六个");
            }

            if (sysBizCarousel.getDisplayed().equals(UpDownShelvesEnum.GROUNDING.getKey()) && !sysBizCarouselService.selectGroundingNum2(sysBizCarousel) && sysBizCarousel.getType().equals(1)) {
                return warn("腰封广告上架最多四个");
            }

            LambdaQueryWrapper<SysBizCarousel> queryWrapper = new LambdaQueryWrapper<SysBizCarousel>();
            queryWrapper.eq(StringUtils.isNotEmpty(sysBizCarousel.getTitle()), SysBizCarousel::getTitle, sysBizCarousel.getTitle());
            queryWrapper.eq(SysBizCarousel::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
            queryWrapper.eq(SysBizCarousel::getTenantId, sysBizCarousel.getTenantId());
            List<SysBizCarousel> sysBizCarousels = sysBizCarouselService.selectSysBizCarouselList(queryWrapper);
            if (!sysBizCarousels.isEmpty()) {
                return warn("标题已存在");
            }
        }
        setTenantId(sysBizCarousel);
        return toAjax(sysBizCarouselService.insertSysBizCarousel(sysBizCarousel));
    }

    /**
     * 修改轮播图管理
     */
    @PreAuthorize("@ss.hasPermi('shop:carousel:edit')")
    @Log(title = "轮播图管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizCarousel sysBizCarousel) {
        if (StringUtils.isNotEmpty(sysBizCarousel.getTitle())) {
            if (sysBizCarousel.getDisplayed().equals(UpDownShelvesEnum.GROUNDING.getKey()) && !sysBizCarouselService.selectGroundingNum(sysBizCarousel) && sysBizCarousel.getType().equals(0)) {
                return warn("轮播图上架最多六个");
            }

            if (sysBizCarousel.getDisplayed().equals(UpDownShelvesEnum.GROUNDING.getKey()) && !sysBizCarouselService.selectGroundingNum2(sysBizCarousel) && sysBizCarousel.getType().equals(1)) {
                return warn("腰封广告上架最多四个");
            }

            LambdaQueryWrapper<SysBizCarousel> queryWrapper = new LambdaQueryWrapper<SysBizCarousel>();
            queryWrapper.eq(StringUtils.isNotEmpty(sysBizCarousel.getTitle()), SysBizCarousel::getTitle, sysBizCarousel.getTitle());
            queryWrapper.ne(SysBizCarousel::getId, sysBizCarousel.getId());
            queryWrapper.eq(SysBizCarousel::getTenantId, sysBizCarousel.getTenantId());
            queryWrapper.eq(SysBizCarousel::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
            List<SysBizCarousel> sysBizCarousels = sysBizCarouselService.selectSysBizCarouselList(queryWrapper);
            if (!sysBizCarousels.isEmpty()) {
                return warn("标题已存在");
            }
        }
        return toAjax(sysBizCarouselService.updateSysBizCarousel(sysBizCarousel));
    }

    /**
     * 删除轮播图管理
     */
    @PreAuthorize("@ss.hasPermi('shop:carousel:remove')")
    @Log(title = "轮播图管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        SysBizCarousel sysBizCarousel = new SysBizCarousel();
        sysBizCarousel.setDeleted(OrderDeleteEnum.DELETE.getKey());
        sysBizCarousel.setId(id);
        return toAjax(sysBizCarouselService.updateSysBizCarousel(sysBizCarousel));
    }
}
